﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Model;
using MM.DBUtility;


namespace MM.DAL
{
    public class T_Users
    {
        public bool Exists(string UserID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_USERS");
            strSql.Append(" where UserID=@UserID ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.VarChar,50)};
            parameters[0].Value = UserID;

            return DbHelperSql.Exists(strSql.ToString(), parameters);
        }

        public void Add(MM.Model.T_Users model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_USERS(");
            strSql.Append("UserID,RoleID,PassWord,UserName,Telephone,Mobile,Address,Remark)");
            strSql.Append(" values (");
            strSql.Append("@UserID,@RoleID,@PassWord,@UserName,@Telephone,@Mobile,@Address,@Remark)");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.VarChar,50),
					new SqlParameter("@RoleID", SqlDbType.Int),
					new SqlParameter("@PassWord", SqlDbType.VarChar,50),
					new SqlParameter("@UserName", SqlDbType.VarChar,200),
					new SqlParameter("@Telephone", SqlDbType.VarChar,20),
					new SqlParameter("@Mobile", SqlDbType.VarChar,15),
					new SqlParameter("@Address", SqlDbType.VarChar,100),
					new SqlParameter("@Remark", SqlDbType.VarChar,500)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.RoleID;
            parameters[2].Value = model.PassWord;
            parameters[3].Value = model.UserName;
            parameters[4].Value = model.Telephone;
            parameters[5].Value = model.Mobile;
            parameters[6].Value = model.Address;
            parameters[7].Value = model.Remark;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Update(MM.Model.T_Users model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Users set ");
            strSql.Append(string.Format("RoleID={0},", model.RoleID));
            strSql.Append("PassWordinfo=@PassWordinfo,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Telephone=@Telephone,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Address=@Address,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where UserID=@UserID ");

            SqlParameter[] parameters = {
                    new SqlParameter("@UserID", SqlDbType.VarChar,50),
					new SqlParameter("@PassWordinfo", SqlDbType.VarChar,50),
                    new SqlParameter("@UserName", SqlDbType.VarChar,200),
                    new SqlParameter("@Telephone", SqlDbType.VarChar,20),
                    new SqlParameter("@Mobile", SqlDbType.VarChar,15),
                    new SqlParameter("@Address", SqlDbType.VarChar,100),
                    new SqlParameter("@Remark", SqlDbType.VarChar,500)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.PassWord;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.Telephone;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.Address;
            parameters[6].Value = model.Remark;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(string UserID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_USERS ");
            strSql.Append(" where UserID=@UserID ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.VarChar,50)};
            parameters[0].Value = UserID;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }

        public MM.Model.T_Users GetCurUser(string UserID)
        {
            MM.Model.T_Users userModel = new MM.Model.T_Users();
            string strSQL = "select * from T_USERS where UserID=@UserID ";
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.VarChar,50)};
            parameters[0].Value = UserID;

            DataSet ds = DbHelperSql.Query(strSQL, parameters);
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    userModel.UserID = row["UserID"].ToString();
                    userModel.PassWord = row["PassWordinfo"].ToString();
                    userModel.RoleID = row["RoleID"] == null ? "" : row["RoleID"].ToString();
                    userModel.UserName = row["UserName"] == null ? "" : row["UserName"].ToString();
                    userModel.Telephone = row["Telephone"] == null ? "" : row["Telephone"].ToString();
                    userModel.Mobile = row["Mobile"] == null ? "" : row["Mobile"].ToString();
                    userModel.Address = row["Address"] == null ? "" : row["Address"].ToString();
                    userModel.Remark = row["Remark"] == null ? "" : row["Remark"].ToString();

                    userModel.CustomerNO = row["CustomerNO"] == null ? "" : row["CustomerNO"].ToString();
                    userModel.ChanelNO = row["ChanelNO"] == null ? "" : row["ChanelNO"].ToString();
                }
            }
            return userModel;
        }

        public static bool CheckExist(string UserID, string PassWord)
        {
            string strSQL = "SELECT COUNT(1) FROM T_Users WHERE UserID=@UserID AND PassWordinfo=@PassWordinfo ";
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.VarChar,50),new SqlParameter("@PassWordinfo", SqlDbType.VarChar,50)};
            parameters[0].Value = UserID;
            parameters[1].Value = PassWord;
            return DbHelperSql.Exists(strSQL, parameters);
        }
    }
}
